package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.AddressBook;

import com.itheima.bean.OrderDetail;
import com.itheima.bean.Orders;
import com.itheima.bean.User;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.dto.OrdersDto;

import com.itheima.service.OrderDetailService;
import com.itheima.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import lombok.extern.slf4j.XSlf4j;

import com.itheima.service.AddressBookService;
import com.itheima.service.OrderDetailService;
import com.itheima.service.OrderService;
import com.itheima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.jni.Address;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;



import java.util.Map;

@Slf4j
@RequestMapping("/order")
@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired

    private OrderDetailService orderDetailService;


    private UserService userService;

    //用户下单
    @Autowired
    private AddressBookService addressBookService;

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        //1.调用service处理
        orderService.submit(orders);

        //2.响应数据 "下单成功"
        return R.success("下单成功！");
    }


    @GetMapping("/page")
    public R page(Integer page,Integer pageSize,String number,String beginTime,String endTime){
        //1.查询Orders表进行前端显示
        Page<Orders> ordersPage = new Page<>();

        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(number!=null&&number.length()>0,Orders::getNumber,number);
        //wrapper.gt(Orders::getOrderTime,beginTime).lt(Orders::getOrderTime,endTime);
        wrapper.between(beginTime!=null&&endTime!=null,Orders::getOrderTime,beginTime,endTime);
        orderService.page(ordersPage,wrapper);


        //1.1：创建OrderDtoPage对象
        Page<OrdersDto> ordersDtoPage = new Page<>();

        //1.2：封装总条数、每页显示条数、当前页码这些数据到setmealDtoPage对象中  忽略records属性的值不进行复制
        BeanUtils.copyProperties(ordersPage,ordersDtoPage,"records");

        //2.封装Dto信息上传完整数据给前端页面
        List<Orders> ordersList = ordersPage.getRecords();

       List<OrdersDto> ordersDtoList = new ArrayList<>();

        for (Orders orders : ordersList) {

            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(orders,ordersDto);
            OrderDetail orderDetail = orderDetailService.getById(orders.getId());
            ordersDto.setId(orderDetail.getId());
        }

        ordersDtoPage.setRecords(ordersDtoList);
        return R.success(ordersPage);
    }

    @GetMapping("/userPage")
    public R<Page<OrdersDto>> pageR(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5")Integer pageSize){
        //1.查询所有用户订单信息
        //1.1获取用户id
        Long currentId = BaseContext.getCurrentId();
        User user = userService.getById(currentId);
//        AddressBook addressBook = addressBookService.getById(currentId);//null
        LambdaQueryWrapper<AddressBook> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(AddressBook::getUserId,currentId);
        wrapper1.eq(AddressBook::getIsDefault,1);
        AddressBook addressBook = addressBookService.getOne(wrapper1);
        //通过id获得用户的所有订单信息
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getUserId,currentId);
        wrapper.orderByDesc(Orders::getOrderTime);
        orderService.page(ordersPage,wrapper);
        //创建完整的页面信息对象
        Page<OrdersDto> ordersDtoPage = new Page<>();
        BeanUtils.copyProperties(ordersPage,ordersDtoPage,"records");
        //获取普通orders表单数据
        List<Orders> ordersList= ordersPage.getRecords();
        List<OrdersDto> ordersDtoList=new ArrayList<>();
        for (Orders orders : ordersList) {
            //获取订单详细数据
            LambdaQueryWrapper<OrderDetail> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.eq(OrderDetail::getOrderId,orders.getId());
            List<OrderDetail> orderDetailList = orderDetailService.list(wrapper2);
            //把订单详细信息拼接到 订单信息中

            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(orders,ordersDto);
            ordersDto.setUserName(user.getName());
            ordersDto.setPhone(user.getPhone());
            ordersDto.setOrderDetails(orderDetailList);
//            ordersDto.setAddress(user.);
            ordersDto.setConsignee(addressBook.getConsignee());

            ordersDtoList.add(ordersDto);
        }
        ordersDtoPage.setRecords(ordersDtoList);
        return R.success(ordersDtoPage);
    }

    //订单配送|完成
    @PutMapping
    public R update(HttpSession session, @RequestBody Orders orders){

        log.info("【OrderController update-线程id：{}】",Thread.currentThread().getId());
        LambdaUpdateWrapper<Orders> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Orders::getId,orders.getId());
        wrapper.set(Orders::getStatus,orders.getStatus());

        //1.调用service完成更新
        boolean flag = orderService.updateById(orders);

        //2.返回处理结果
        if(flag){
            return R.success("信息修改成功！");
        }else{
            return R.error("信息修改失败！");
        }
    }

}
